package com.badibadi.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.json.JSONException;

import com.badibadi.fragment.ActivityOverViewFragment0;
import com.badibadi.fragment.CalendarFragment;
import com.badibadi.fragment.CalendarFragment.CalendarGridViewAdapter;
import com.badibadi.fragment.CalendarFragment.getHashMap;
import com.badibadi.infos.Activity_Overview_Model;
import com.badibadi.infos.Results;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.badibadi.uniclubber.R.id;
import com.badibadi.uniclubber.R.string;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 期间内的活动
 * 
 * @author Administrator
 * 
 */
public class CalendarBetweenActivity extends CalendarActivity {
	private int StartYY, StartMM, StartDD;
	private int EndYY, EndMM, EndDD;
	private int Judge_YY, Judge_MM, Judge_DD;
	private String areacontent = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		areacontent = getIntent().getStringExtra(
				getResources().getString(R.string.l_xb37));
		// li_activity_show();
		init();
	}

	/**
	 * 重写日历的侦听接口
	 */
	@Override
	protected void initOnCalendarItemClistener(AdapterView<?> arg0, View view,
			int position, long arg3, CalendarGridViewAdapter calendaradapter) {
		// TODO Auto-generated method stub
		// calendaradapter.getYearMonthDay(position);
		if (backdd(Constants.temp_map,
				calendaradapter.getYearMonthDay(position)).size() > 0) {
			Intent intent = new Intent(CalendarBetweenActivity.this,
					Data_During_Activity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.putExtra(
					"日期",
					back_data(calendaradapter.getYearMonthDay(position), 0)
							+ "."
							+ back_data(
									calendaradapter.getYearMonthDay(position),
									1)
							+ "."
							+ back_data(
									calendaradapter.getYearMonthDay(position),
									2));
			startActivity(intent);
		} else {

		}
		// Intent intent = new Intent(CalendarBetweenActivity.this,
		// RecordListDayRememberActivity.class);
		// startActivity(intent);
	}

	@Override
	protected void initCalendarFragment(
			final List<Activity_Overview_Model> models) {
		// TODO Auto-generated method stub
		tabhost.setVisibility(View.GONE);
		Bundle bundle = new Bundle();
		FragmentTransaction transaction = manager.beginTransaction();
		calendarFragment = CalendarFragment.getInstance();
		calendarFragment.setIs_betweenActivity(true);
		calendarFragment
				.setOnCalendarOnClistener(new CalendarFragment.CalendarListener() {

					@Override
					public boolean AddStartButton(int Year, int Month, int Day) {
						// TODO Auto-generated method stub
						return ShowStartButton(Year, Month, Day, StartYY,
								StartMM, StartDD);
					}

					@Override
					public boolean AddEndButton(int Year, int Month, int Day) {
						// TODO Auto-generated method stub
						return ShowEndButton(Year, Month, Day, EndYY, EndMM,
								EndDD);
					}

					@Override
					public String AddActivity(int Year, int Month, int Day) {
						// TODO Auto-generated method stub

						// if (Year == 2014 && Month == 8 && Day == 8) {
						// return "是福建省懂了否";
						// }
						// if (Year == 2014 && Month == 8 && Day == 9) {
						// return "是福建省懂了";
						// }
						return ShowActivity(Year, Month, Day, models);
					}
				});
		calendarFragment
				.setOnCalendarItemClistener(new CalendarFragment.CalendarGridViewListener() {
					@Override
					public void ItemListener(AdapterView<?> arg0, View view,
							int position, long arg3,
							CalendarGridViewAdapter calendaradapter) {
						// TODO Auto-generated method stub
						initOnCalendarItemClistener(arg0, view, position, arg3,
								calendaradapter);
					}
				});
		bundle.putInt("year", BackYear(Constants.START_TIME));
		bundle.putInt("month", BackMonth(Constants.START_TIME));
		calendarFragment.setArguments(bundle);
		transaction.replace(R.id.calendar_layout, calendarFragment);
		transaction.commit();
	}

	@Override
	public void initListFragment(int t, String yes) {
		// TODO Auto-generated method stub
		super.initListFragment(t, "yes");
	}

	/**
	 * 匹配当前时间是否有活动
	 * 
	 * @param map匹配的map
	 * @param time传入需要匹配的时间
	 */
	private List<Integer> backdd(Map<String, Object> map, String time) {
		Set<Map.Entry<String, Object>> set = map.entrySet();
		// boolean jud = false;
		List<Integer> integers = new ArrayList<Integer>();
		for (Iterator<Map.Entry<String, Object>> it = set.iterator(); it
				.hasNext();) {
			Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it
					.next();
			//    (entry.getKey() + "--->" +
			// entry.getValue());
			if (Utils.getStringMillis(time) == Utils.getStringMillis(entry
					.getKey())) {
				// jud = true;
				integers = (List<Integer>) entry.getValue();
			}
		}
		Constants.temp_num = integers;
		return integers;
	}

	private String back_data(String dt, int s) {
		String dt_[] = dt.split("-");
		return dt_[s];
	}

	private void init() {
		((TextView) (this.findViewById(R.id.calendar_title)))
				.setText(getResources().getString(R.string.BetweenActivity));
		// GoneAllTabHost();
	}

	// @Override
	// protected void GoneAllTabHost() {
	// // TODO Auto-generated method stub
	// super.GoneAllTabHost();
	// }

	@Override
	protected void ChangeToThreeTab() {
		// TODO Auto-generated method stub
		super.ChangeToThreeTab();
		tabhost2.setVisibility(View.GONE);
	}

	/**
	 * 显示开始按钮的button
	 */
	@Override
	protected boolean ShowStartButton(int Y, int M, int D, int YY, int MM,
			int DD) {
		// TODO Auto-generated method stub
		//    ("开始时间" + Constants.START_TIME);
		return super.ShowStartButton(Y, M, D, BackYear(Constants.START_TIME),
				BackMonth(Constants.START_TIME), BackDay(Constants.START_TIME));
	}

	/**
	 * 显示结束按钮的button
	 */
	@Override
	protected boolean ShowEndButton(int Y, int M, int D, int YY, int MM, int DD) {
		// TODO Auto-generated method stub
		//    ("结束时间" + Constants.END_TIME);
		return super.ShowEndButton(Y, M, D, BackYear(Constants.END_TIME),
				BackMonth(Constants.END_TIME), BackDay(Constants.END_TIME));
	}

	/**
	 * 在这里返回活动内容
	 */
	@Override
	public String ShowActivity(int Y, int M, int D,
			List<Activity_Overview_Model> models) {
		// TODO Auto-generated method stub
		return super.ShowActivity(Y, M, D, Constants.overview_Models);
	}

	/**
	 * 看第0个是什么
	 */
	@Override
	public void OnChageTabHost0() {
		// TODO Auto-generated method stub
		GoneAllTabHost();
	}

	@Override
	public void initTabHostTHREE(int id, int id2, int id3) {
		// TODO Auto-generated method stub
		super.initTabHostTHREE(id, id2, R.string.Nearby);
	}

}
